package top.yxf.spring.beans.factory.support;

import top.yxf.spring.beans.BeansException;
import top.yxf.spring.beans.factory.BeanFactory;
import top.yxf.spring.beans.factory.FactoryBean;
import top.yxf.spring.beans.factory.config.BeanDefinition;
import top.yxf.spring.beans.factory.config.BeanPostProcessor;
import top.yxf.spring.beans.factory.config.ConfigurableBeanFactory;
import top.yxf.spring.util.ClassUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author xs.wu
 */
public abstract class AbstractBeanFactory extends FactoryBeanRegistrySupport implements ConfigurableBeanFactory {

    /** ClassLoader to resolve bean class names with, if necessary */
    private ClassLoader beanClassLoader = ClassUtils.getDefaultClassLoader();
    /** BeanPostProcessors to apply in createBean */
    private final List<BeanPostProcessor> beanPostProcessors = new ArrayList<BeanPostProcessor>();

    @Override
    public Object getBean(String name) throws BeansException {
        return doGetBean(name, null);
    }

    @Override
    public Object getBean(String name, Object... args) throws BeansException {
        return doGetBean(name, args);
    }

    @Override
    public <T> T getBean(String name, Class<T> requiredType) throws BeansException {
        return (T) getBean(name);
    }

    protected <T> T doGetBean(String beanName, Object ... args) {
        // 先获取单例
        Object sharedInstance = getSingleton(beanName);
        if (sharedInstance != null) {
            return (T) getObjectForBeanInstance(sharedInstance,beanName);
        }
        BeanDefinition beanDefinition = getBeanDefinition(beanName);
        Object bean = createBean(beanName, beanDefinition, args);
        return (T) getObjectForBeanInstance(bean, beanName);
    }

    @Override
    public void addBeanPostProcessor(BeanPostProcessor beanPostProcessor) {
        this.beanPostProcessors.remove(beanPostProcessor);
        this.beanPostProcessors.add(beanPostProcessor);
    }


    public Object getObjectForBeanInstance(Object beanInstance, String beanName) {
        if (!(beanInstance instanceof FactoryBean)) {
            return beanInstance;
        }
        Object o = getCachedObjectForFactoryBean(beanName);
        if (o == null) {
            FactoryBean<?> factoryBean = (FactoryBean<?>) beanInstance;
            o = getObjectFromFactoryBean(factoryBean, beanName);
        }

        return o;
    }

    public ClassLoader getBeanClassLoader() {
        return this.beanClassLoader;
    }

    /**
     * Return the list of BeanPostProcessors that will get applied
     * to beans created with this factory.
     */
    public List<BeanPostProcessor> getBeanPostProcessors() {
        return this.beanPostProcessors;
    }




    /**
     * 获取BeanDefinition对象
     * @param beanName
     * @return
     * @throws BeansException
     */
    protected abstract BeanDefinition getBeanDefinition(String beanName) throws BeansException;

    /**
     * 创建bean
     * @param beanName bean 名称
     * @param beanDefinition bean中的一些核心属性 如(beanClass、scope、isLazy、dependsOn、primary、initMethodName）
     * @return
     * @throws BeansException
     */
    protected abstract Object createBean(String beanName, BeanDefinition beanDefinition, Object[] args) throws BeansException;
}
